#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
//整体布局
    mainwidget=new QWidget(this);//主窗口
    setCentralWidget(mainwidget);

    graphicsview=new QGraphicsView(mainwidget);//游戏场景窗口
    scene=new QGraphicsScene;
    scene->setSceneRect(0,0,BOARD_WIDTH*BLOCK_SIZE,BOARD_HEIGHT*BLOCK_SIZE);
    graphicsview->setScene(scene);
    graphicsview->setFixedSize(scene->width(),scene->height());
    QRectF rcontent=graphicsview->contentsRect();//防止出现滚动条
    graphicsview->setSceneRect(rcontent);

    nextgraphicsview=new QGraphicsView(mainwidget);//显示下一个方块
    nextscene=new QGraphicsScene;
    nextscene->setSceneRect(0,0,BLOCK_SIZE*6,BLOCK_SIZE*6);//6*6个正方形的显示界面
    nextgraphicsview->setScene(nextscene);
    nextgraphicsview->setFixedSize(nextscene->width(),nextscene->height());
    QRectF nextrcontent=nextgraphicsview->contentsRect();//防止出现滚动条
    nextgraphicsview->setSceneRect(nextrcontent);

    scorelabel=new QLabel(QString("  Score ：%1").arg(0),mainwidget);//得分显示
    scorelabel->setFixedSize(200,50);//标签大小
    scorelabel->setStyleSheet("background-color:green;color:black;font-size:20px;font-family:Garamond;");

    gamedifficulty=new QSpinBox(mainwidget);//游戏难度
    gamedifficulty->setRange(1,6);//范围
    gamedifficulty->setSingleStep(1);//一次改变1
    gamedifficulty->setValue(1);//初始难度
    //gamedifficulty->setReadOnly(true);//禁止文本直接编辑
    gamedifficulty->setFixedSize(200,50);//微调框大小
    gamedifficulty->setStyleSheet("background-color:pink;color:black;font-size:20px;font-family:Garamond;");

    buttonwidget=new QWidget(mainwidget);//控制窗口
    gamemodebutton=new QPushButton("普通模式",buttonwidget);
    gamemodebutton->setStyleSheet("background-color:gray;color:blue;font-size:20px;font-family:STXihei;");
    gamemodebutton->setCursor(Qt::PointingHandCursor);//手型
    gamemodebutton->setFixedSize(200,50);//按钮大小

    startbutton=new QPushButton("开始游戏",buttonwidget);
    startbutton->setStyleSheet("background-color:black;color:white;font-size:20px;font-family:KaiTi;");
    startbutton->setCursor(Qt::PointingHandCursor);//手型
    startbutton->setFixedSize(200,100);//按钮大小

    stopbutton=new QPushButton("暂停游戏",buttonwidget);
    stopbutton->setStyleSheet("background-color:red;color:white;font-size:20px;font-family:KaiTi;");
    stopbutton->setCursor(Qt::PointingHandCursor);
    stopbutton->setFixedSize(200,100);


    overbutton=new QPushButton("结束游戏",buttonwidget);
    overbutton->setStyleSheet("background-color:blue;color:white;font-size:20px;font-family:KaiTi;");
    overbutton->setCursor(Qt::PointingHandCursor);
    overbutton->setFixedSize(200,100);


    QVBoxLayout *vlayout=new QVBoxLayout(buttonwidget);
    buttonwidget->setLayout(vlayout);
    vlayout->addWidget(nextgraphicsview);
    vlayout->addWidget(scorelabel);
    vlayout->addWidget(gamedifficulty);
    vlayout->addWidget(gamemodebutton);
    vlayout->addWidget(startbutton);
    vlayout->addWidget(stopbutton);
    vlayout->addWidget(overbutton);

    QHBoxLayout *hlayout=new QHBoxLayout(mainwidget);
    mainwidget->setLayout(hlayout);
    hlayout->addWidget(graphicsview);
    hlayout->addWidget(buttonwidget);

//初始化方块形状
    SHAPES1 = {
        //点型
        {
            {{0, 0}},
            {{0, 0}},
            {{0, 0}},
            {{0, 0}}
        }
    };
    SHAPES2 = {
        //
        //点型
        {
            {{0, 0}},
            {{0, 0}},
            {{0, 0}},
            {{0, 0}}
        },
        //
        //两点型
        {
            {{0, 0}, {1, 0}},   //水平
            {{0, 0}, {0, 1}},   //垂直
            {{0, 0}, {1, 0}},   //水平
            {{0, 0}, {0, 1}}    //垂直
        },
        //
        //三点型
        {
            {{0, 0}, {-1, 0},{1, 0}},   //水平
            {{0, 1}, {0, 0},{0, 2}},    //垂直
            {{0, 0}, {-1, 0},{1, 0}},   //水平
            {{0, 1}, {0, 0},{0, 2}}     //垂直
        },
        //墙角型
        {
            {{0, 0}, {1, 0},{0, 1}},   //墙角左上
            {{1, 0}, {0, 0},{1, 1}},   //墙角右上
            {{1, 1}, {1, 0},{0, 1}},   //墙角右下
            {{0, 1}, {0, 0},{1, 1}}    //墙角左下
        }
    };
    SHAPES3 = {
        // I型（2种旋转）
        {
            {{0, 0}, {-1, 0}, {1, 0}, {2, 0}},   // 水平
            {{0, 1}, {0, 0}, {0, 2}, {0, 3}},    // 垂直
            {{0, 0}, {-1, 0}, {1, 0}, {2, 0}},   // 水平
            {{0, 1}, {0, 0}, {0, 2}, {0, 3}}     // 垂直
        },
        // O型（1种旋转）
        {
            {{0, 0}, {1, 0}, {0, 1}, {1, 1}},     // 正方形
            {{0, 0}, {1, 0}, {0, 1}, {1, 1}},     // 正方形
            {{0, 0}, {1, 0}, {0, 1}, {1, 1}},     // 正方形
            {{0, 0}, {1, 0}, {0, 1}, {1, 1}}      // 正方形
        },
        // T型（4种旋转）
        {
            {{0, 0}, {-1, 0}, {1, 0}, {0, 1}},    // T向下
            {{0, 1}, {0, 0}, {0, 2}, {-1, 1}},    // T向左
            {{0, 1}, {-1, 1}, {1, 1}, {0, 0}},    // T向上
            {{0, 1}, {0, 0}, {0, 2}, {1, 1}}      // T向右
        },
        // S型（2种旋转)
        {
            {{0, 1}, {-1, 1}, {0, 0}, {1, 0}},    // S水平
            {{0, 1}, {0, 0}, {1, 1}, {1, 2}},     // S垂直
            {{0, 1}, {-1, 1}, {0, 0}, {1, 0}},    // S水平
            {{0, 1}, {0, 0}, {1, 1}, {1, 2}}      // S垂直
        },
        // Z型（2种旋转）
        {
            {{0, 1}, {1, 1}, {0, 0}, {-1, 0}},    // Z水平
            {{0, 1}, {0, 2}, {1, 1}, {1, 0}},     // Z垂直
            {{0, 1}, {1, 1}, {0, 0}, {-1, 0}},    // Z水平
            {{0, 1}, {0, 2}, {1, 1}, {1, 0}}      // Z垂直
        },
        //J型（4种旋转）
        {
            {{1, 1}, {1, 0}, {1, 2}, {0, 2}},    // J向左
            {{0, 1}, {1, 1}, {-1, 1}, {-1, 0}},  // J向上
            {{0, 1}, {0, 2}, {0, 0}, {1, 0}},    // J向右
            {{0, 0}, {-1, 0}, {1, 0}, {1, 1}}    // J向下
        },
        // L型（4种旋转）
        {
            {{0, 1}, {0, 0}, {0, 2}, {1, 2}},   // L向右
            {{0, 1}, {1, 1}, {-1, 1}, {-1, 2}}, // L向下
            {{1, 1}, {1, 2}, {1, 0}, {0, 0}},   // L向左
            {{0, 1}, {-1, 1}, {1, 1}, {1, 0}}   // L向上
        }
    };
    SHAPES4 = {
        //                                              形状
        {
            {{0, 0}, {-1, 0}, {-2, 0}, {1, 0},{2, 0}},   // // // // //
            {{0, 2}, {0, 0}, {0, 1}, {0, 3},{0, 4}},
            {{0, 0}, {-1, 0}, {-2, 0}, {1, 0},{2, 0}},
            {{0, 2}, {0, 0}, {0, 1}, {0, 3},{0, 4}}
        },
        //                                              形状
        {
            {{1, 1}, {2, 0}, {2, 1}, {0, 1},{-1, 1}},            //
            {{0, 2}, {1, 3}, {0, 3}, {0, 1},{0, 0}},    // // // //
            {{0, 0}, {-1, 1}, {-1, 0}, {1, 0},{2, 0}},
            {{1, 1}, {0, 0}, {1, 0}, {1, 2},{1, 3}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 0}, {-1, 1}, {1, 1},{2, 1}},  //
            {{0, 1}, {1, 0}, {0, 0}, {0, 2},{0, 3}},    // // // //
            {{1, 0}, {2, 1}, {2, 0}, {0, 0},{-1, 0}},
            {{1, 2}, {0, 3}, {1, 3}, {1, 1},{1, 0}}
        },
        //                                              形状
        {
            {{1, 1}, {1, 0}, {2, 1}, {0, 1},{-1, 1}},         //
            {{0, 2}, {1, 2}, {0, 3}, {0, 1},{0, 0}},    // // // //
            {{0, 0}, {0, 1}, {-1, 0}, {1, 0},{2, 0}},
            {{1, 1}, {0, 1}, {1, 0}, {1, 2},{1, 3}}
        },
        //                                              形状
        {
            {{0, 1}, {0, 0}, {-1, 1}, {1, 1},{2, 1}},      //
            {{0, 1}, {1, 1}, {0, 0}, {0, 2},{0, 3}},    // // // //
            {{1, 0}, {1, 1}, {2, 0}, {0, 0},{-1, 0}},
            {{1, 2}, {0, 2}, {1, 3}, {1, 1},{1, 0}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 1}, {1, 1}, {0, 0},{1, 0}},      // //
            {{0, 1}, {0, 0}, {0, 2}, {1, 1},{1, 2}},    // // //
            {{0, 0}, {1, 0}, {-1, 0}, {0, 1},{-1, 1}},
            {{1, 1}, {1, 2}, {1, 0}, {0, 1},{0, 0}}
        },
        //                                              形状
        {
            {{0, 1}, {1, 1}, {-1, 1}, {0, 0},{-1, 0}},   // //
            {{0, 1}, {0, 2}, {0, 0}, {1, 1},{1, 0}},     // // //
            {{0, 0}, {-1, 0}, {1, 0}, {0, 1},{1, 1}},
            {{1, 1}, {1, 0}, {1, 2}, {0, 1},{0, 2}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 0}, {-1, 1}, {1, 1},{1, 0}},   //    //
            {{0, 1}, {1, 0}, {0, 0}, {0, 2},{1, 2}},     // // //
            {{0, 0}, {1, 1}, {1, 0}, {-1, 0},{-1, 1}},
            {{1, 1}, {0, 2}, {1, 2}, {1, 0},{0, 0}}
        },
        //                                              形状
        {
            {{0, 1}, {1, 0}, {1, 1}, {-1, 1},{-1, 2}},          //
            {{0, 1}, {1, 2}, {0, 2}, {0, 0},{-1, 0}},     // // //
            {{0, 1}, {1, 0}, {1, 1}, {-1, 1},{-1, 2}},    //
            {{0, 1}, {1, 2}, {0, 2}, {0, 0},{-1, 0}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 0}, {-1, 1}, {1, 1},{1, 2}},    //
            {{0, 1}, {1, 0}, {0, 0}, {0, 2},{-1, 2}},     // // //
            {{0, 1}, {-1, 0}, {-1, 1}, {1, 1},{1, 2}},          //
            {{0, 1}, {1, 0}, {0, 0}, {0, 2},{-1, 2}}
        },
        //                                              形状
        {
            {{0, 1}, {1, 0}, {1, 1}, {-1, 1},{0, 2}},           //
            {{0, 1}, {1, 2}, {0, 2}, {0, 0},{-1, 1}},     // // //
            {{0, 1}, {-1, 2}, {-1, 1}, {1, 1},{0, 0}},       //
            {{0, 1}, {-1, 0}, {0, 0}, {0, 2},{1, 1}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 0}, {-1, 1}, {1, 1},{0, 2}},    //
            {{0, 1}, {1, 0}, {0, 0}, {0, 2},{-1, 1}},     // // //
            {{0, 1}, {1, 2}, {1, 1}, {-1, 1},{0, 0}},        //
            {{0, 1}, {-1, 2}, {0, 2}, {0, 0},{1, 1}}
        },
        //                                              形状
        {
            {{0, 1}, {1, 1}, {-1, 0}, {-1, 1},{-1, 2}},    //
            {{0, 1}, {0, 2}, {1, 0}, {0, 0},{-1, 0}},      // // //
            {{0, 1}, {-1, 1}, {1, 2}, {1, 1},{1, 0}},      //
            {{0, 1}, {0, 0}, {-1, 2}, {0, 2},{1, 2}}
        },
        //                                              形状
        {
            {{0, 1}, {0, 0}, {0, 2}, {-1, 1},{1, 1}},        //
            {{0, 1}, {0, 0}, {0, 2}, {-1, 1},{1, 1}},     // // //
            {{0, 1}, {0, 0}, {0, 2}, {-1, 1},{1, 1}},        //
            {{0, 1}, {0, 0}, {0, 2}, {-1, 1},{1, 1}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 0}, {0, 0}, {1, 1},{2, 1}},    // //
            {{0, 1}, {1, 0}, {1, 1}, {0, 2},{0, 3}},        // // //
            {{1, 0}, {2, 1}, {1, 1}, {0, 0},{-1, 0}},
            {{1, 2}, {0, 3}, {0, 2}, {1, 1},{1, 0}}
        },
        //                                              形状
        {
            {{1, 1}, {2, 0}, {1, 0}, {0, 1},{-1, 1}},          // //
            {{0, 2}, {1, 3}, {1, 2}, {0, 1},{0, 0}},     // // //
            {{0, 0}, {-1, 1}, {0, 1}, {1, 0},{2, 0}},
            {{1, 1}, {0, 0}, {0, 1}, {1, 2},{1, 3}}
        },
        //                                              形状
        {
            {{-1, 2}, {-1, 0}, {-1, 1}, {0, 2},{1, 2}},     //
            {{-1, 0}, {1, 0}, {0, 0}, {-1, 1},{-1, 2}},     //
            {{1, 0}, {-1, 0}, {0, 0}, {1, 1},{1, 2}},       // // //
            {{1, 2}, {1, 0}, {1, 1}, {0, 2},{-1, 2}}
        },
        //                                              形状
        {
            {{0, 1}, {1, 0}, {0, 0}, {-1, 1},{-1, 2}},         // //
            {{0, 1}, {1, 2}, {1, 1}, {0, 0},{-1, 0}},       // //
            {{0, 1}, {-1, 2}, {0, 2}, {1, 1},{1, 0}},       //
            {{0, 1}, {-1, 0}, {-1, 1}, {0, 2},{1, 2}}
        }
    };
    SHAPES5 = {
        //
        //点型
        {
            {{0, 0}},
            {{0, 0}},
            {{0, 0}},
            {{0, 0}}
        },
        //
        //两点型
        {
            {{0, 0}, {1, 0}},   //水平
            {{0, 0}, {0, 1}},   //垂直
            {{0, 0}, {1, 0}},   //水平
            {{0, 0}, {0, 1}}    //垂直
        },
        //三点型
        {
            {{0, 0}, {-1, 0},{1, 0}},   //水平
            {{0, 1}, {0, 0},{0, 2}},    //垂直
            {{0, 0}, {-1, 0},{1, 0}},   //水平
            {{0, 1}, {0, 0},{0, 2}}     //垂直
        },
        //墙角型
        {
            {{0, 0}, {1, 0},{0, 1}},   //墙角左上
            {{1, 0}, {0, 0},{1, 1}},   //墙角右上
            {{1, 1}, {1, 0},{0, 1}},   //墙角右下
            {{0, 1}, {0, 0},{1, 1}}    //墙角左下
        },
        //
        // I型（2种旋转）
        {
            {{0, 0}, {-1, 0}, {1, 0}, {2, 0}},   // 水平
            {{0, 1}, {0, 0}, {0, 2}, {0, 3}},    // 垂直
            {{0, 0}, {-1, 0}, {1, 0}, {2, 0}},   // 水平
            {{0, 1}, {0, 0}, {0, 2}, {0, 3}}     // 垂直
        },
        // O型（1种旋转）
        {
            {{0, 0}, {1, 0}, {0, 1}, {1, 1}},     // 正方形
            {{0, 0}, {1, 0}, {0, 1}, {1, 1}},     // 正方形
            {{0, 0}, {1, 0}, {0, 1}, {1, 1}},     // 正方形
            {{0, 0}, {1, 0}, {0, 1}, {1, 1}}      // 正方形
        },
        // T型（4种旋转）
        {
            {{0, 0}, {-1, 0}, {1, 0}, {0, 1}},    // T向下
            {{0, 1}, {0, 0}, {0, 2}, {-1, 1}},    // T向左
            {{0, 1}, {-1, 1}, {1, 1}, {0, 0}},    // T向上
            {{0, 1}, {0, 0}, {0, 2}, {1, 1}}      // T向右
        },
        // S型（2种旋转)
        {
            {{0, 1}, {-1, 1}, {0, 0}, {1, 0}},    // S水平
            {{0, 1}, {0, 0}, {1, 1}, {1, 2}},     // S垂直
            {{0, 1}, {-1, 1}, {0, 0}, {1, 0}},    // S水平
            {{0, 1}, {0, 0}, {1, 1}, {1, 2}}      // S垂直
        },
        // Z型（2种旋转）
        {
            {{0, 1}, {1, 1}, {0, 0}, {-1, 0}},    // Z水平
            {{0, 1}, {0, 2}, {1, 1}, {1, 0}},     // Z垂直
            {{0, 1}, {1, 1}, {0, 0}, {-1, 0}},    // Z水平
            {{0, 1}, {0, 2}, {1, 1}, {1, 0}}      // Z垂直
        },
        //J型（4种旋转）
        {
            {{1, 1}, {1, 0}, {1, 2}, {0, 2}},    // J向左
            {{0, 1}, {1, 1}, {-1, 1}, {-1, 0}},  // J向上
            {{0, 1}, {0, 2}, {0, 0}, {1, 0}},    // J向右
            {{0, 0}, {-1, 0}, {1, 0}, {1, 1}}    // J向下
        },
        // L型（4种旋转）
        {
            {{0, 1}, {0, 0}, {0, 2}, {1, 2}},   // L向右
            {{0, 1}, {1, 1}, {-1, 1}, {-1, 2}}, // L向下
            {{1, 1}, {1, 2}, {1, 0}, {0, 0}},   // L向左
            {{0, 1}, {-1, 1}, {1, 1}, {1, 0}}   // L向上
        },
        //
        //                                              形状
        {
            {{0, 0}, {-1, 0}, {-2, 0}, {1, 0},{2, 0}},   // // // // //
            {{0, 2}, {0, 0}, {0, 1}, {0, 3},{0, 4}},
            {{0, 0}, {-1, 0}, {-2, 0}, {1, 0},{2, 0}},
            {{0, 2}, {0, 0}, {0, 1}, {0, 3},{0, 4}}
        },
        //                                              形状
        {
            {{1, 1}, {2, 0}, {2, 1}, {0, 1},{-1, 1}},            //
            {{0, 2}, {1, 3}, {0, 3}, {0, 1},{0, 0}},    // // // //
            {{0, 0}, {-1, 1}, {-1, 0}, {1, 0},{2, 0}},
            {{1, 1}, {0, 0}, {1, 0}, {1, 2},{1, 3}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 0}, {-1, 1}, {1, 1},{2, 1}},  //
            {{0, 1}, {1, 0}, {0, 0}, {0, 2},{0, 3}},    // // // //
            {{1, 0}, {2, 1}, {2, 0}, {0, 0},{-1, 0}},
            {{1, 2}, {0, 3}, {1, 3}, {1, 1},{1, 0}}
        },
        //                                              形状
        {
            {{1, 1}, {1, 0}, {2, 1}, {0, 1},{-1, 1}},         //
            {{0, 2}, {1, 2}, {0, 3}, {0, 1},{0, 0}},    // // // //
            {{0, 0}, {0, 1}, {-1, 0}, {1, 0},{2, 0}},
            {{1, 1}, {0, 1}, {1, 0}, {1, 2},{1, 3}}
        },
        //                                              形状
        {
            {{0, 1}, {0, 0}, {-1, 1}, {1, 1},{2, 1}},      //
            {{0, 1}, {1, 1}, {0, 0}, {0, 2},{0, 3}},    // // // //
            {{1, 0}, {1, 1}, {2, 0}, {0, 0},{-1, 0}},
            {{1, 2}, {0, 2}, {1, 3}, {1, 1},{1, 0}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 1}, {1, 1}, {0, 0},{1, 0}},      // //
            {{0, 1}, {0, 0}, {0, 2}, {1, 1},{1, 2}},    // // //
            {{0, 0}, {1, 0}, {-1, 0}, {0, 1},{-1, 1}},
            {{1, 1}, {1, 2}, {1, 0}, {0, 1},{0, 0}}
        },
        //                                              形状
        {
            {{0, 1}, {1, 1}, {-1, 1}, {0, 0},{-1, 0}},   // //
            {{0, 1}, {0, 2}, {0, 0}, {1, 1},{1, 0}},     // // //
            {{0, 0}, {-1, 0}, {1, 0}, {0, 1},{1, 1}},
            {{1, 1}, {1, 0}, {1, 2}, {0, 1},{0, 2}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 0}, {-1, 1}, {1, 1},{1, 0}},   //    //
            {{0, 1}, {1, 0}, {0, 0}, {0, 2},{1, 2}},     // // //
            {{0, 0}, {1, 1}, {1, 0}, {-1, 0},{-1, 1}},
            {{1, 1}, {0, 2}, {1, 2}, {1, 0},{0, 0}}
        },
        //                                              形状
        {
            {{0, 1}, {1, 0}, {1, 1}, {-1, 1},{-1, 2}},          //
            {{0, 1}, {1, 2}, {0, 2}, {0, 0},{-1, 0}},     // // //
            {{0, 1}, {1, 0}, {1, 1}, {-1, 1},{-1, 2}},    //
            {{0, 1}, {1, 2}, {0, 2}, {0, 0},{-1, 0}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 0}, {-1, 1}, {1, 1},{1, 2}},    //
            {{0, 1}, {1, 0}, {0, 0}, {0, 2},{-1, 2}},     // // //
            {{0, 1}, {-1, 0}, {-1, 1}, {1, 1},{1, 2}},          //
            {{0, 1}, {1, 0}, {0, 0}, {0, 2},{-1, 2}}
        },
        //                                              形状
        {
            {{0, 1}, {1, 0}, {1, 1}, {-1, 1},{0, 2}},           //
            {{0, 1}, {1, 2}, {0, 2}, {0, 0},{-1, 1}},     // // //
            {{0, 1}, {-1, 2}, {-1, 1}, {1, 1},{0, 0}},       //
            {{0, 1}, {-1, 0}, {0, 0}, {0, 2},{1, 1}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 0}, {-1, 1}, {1, 1},{0, 2}},    //
            {{0, 1}, {1, 0}, {0, 0}, {0, 2},{-1, 1}},     // // //
            {{0, 1}, {1, 2}, {1, 1}, {-1, 1},{0, 0}},        //
            {{0, 1}, {-1, 2}, {0, 2}, {0, 0},{1, 1}}
        },
        //                                              形状
        {
            {{0, 1}, {1, 1}, {-1, 0}, {-1, 1},{-1, 2}},    //
            {{0, 1}, {0, 2}, {1, 0}, {0, 0},{-1, 0}},      // // //
            {{0, 1}, {-1, 1}, {1, 2}, {1, 1},{1, 0}},      //
            {{0, 1}, {0, 0}, {-1, 2}, {0, 2},{1, 2}}
        },
        //                                              形状
        {
            {{0, 1}, {0, 0}, {0, 2}, {-1, 1},{1, 1}},        //
            {{0, 1}, {0, 0}, {0, 2}, {-1, 1},{1, 1}},     // // //
            {{0, 1}, {0, 0}, {0, 2}, {-1, 1},{1, 1}},        //
            {{0, 1}, {0, 0}, {0, 2}, {-1, 1},{1, 1}}
        },
        //                                              形状
        {
            {{0, 1}, {-1, 0}, {0, 0}, {1, 1},{2, 1}},    // //
            {{0, 1}, {1, 0}, {1, 1}, {0, 2},{0, 3}},        // // //
            {{1, 0}, {2, 1}, {1, 1}, {0, 0},{-1, 0}},
            {{1, 2}, {0, 3}, {0, 2}, {1, 1},{1, 0}}
        },
        //                                              形状
        {
            {{1, 1}, {2, 0}, {1, 0}, {0, 1},{-1, 1}},          // //
            {{0, 2}, {1, 3}, {1, 2}, {0, 1},{0, 0}},     // // //
            {{0, 0}, {-1, 1}, {0, 1}, {1, 0},{2, 0}},
            {{1, 1}, {0, 0}, {0, 1}, {1, 2},{1, 3}}
        },
        //                                              形状
        {
            {{-1, 2}, {-1, 0}, {-1, 1}, {0, 2},{1, 2}},     //
            {{-1, 0}, {1, 0}, {0, 0}, {-1, 1},{-1, 2}},     //
            {{1, 0}, {-1, 0}, {0, 0}, {1, 1},{1, 2}},       // // //
            {{1, 2}, {1, 0}, {1, 1}, {0, 2},{-1, 2}}
        },
        //                                              形状
        {
            {{0, 1}, {1, 0}, {0, 0}, {-1, 1},{-1, 2}},         // //
            {{0, 1}, {1, 2}, {1, 1}, {0, 0},{-1, 0}},       // //
            {{0, 1}, {-1, 2}, {0, 2}, {1, 1},{1, 0}},       //
            {{0, 1}, {-1, 0}, {-1, 1}, {0, 2},{1, 2}}
        }
    };


//初始为普通模式
    SHAPES=SHAPES3;

// 初始化游戏板
    gameBoard.resize(BOARD_HEIGHT);
    for(int y=0; y<BOARD_HEIGHT; ++y) {
        gameBoard[y].resize(BOARD_WIDTH);
    }//20*25个正方形的游戏界面

// 游戏定时器
    gameTimer = new QTimer(this);
    connect(gameTimer, &QTimer::timeout, this, &MainWindow::moveDown);//定时下落

// 按钮连接
    connect(gamemodebutton,&QPushButton::clicked,this,&MainWindow::ongamemodebuttonclicked);
    connect(startbutton, &QPushButton::clicked,this,&MainWindow::onstartbuttonclicked);
    connect(stopbutton, &QPushButton::clicked, this,&MainWindow::onstopbuttonclicked);
    connect(overbutton, &QPushButton::clicked,this,&MainWindow::onoverbuttonclicked);

//分数显示连接
    connect(this,&MainWindow::scorechanged,this,&MainWindow::updatescorelabel);

//难度更新连接
    connect(gamedifficulty,&QSpinBox::valueChanged,this,&MainWindow::gamedifficultychanged);

// 初始化随机种子
    srand(time(nullptr));

//设置主窗口焦点
    setFocusPolicy(Qt::StrongFocus);
    setFocus();
//确保键盘事件生效

//禁止按钮获取焦点
    startbutton->setFocusPolicy(Qt::NoFocus);
    stopbutton->setFocusPolicy(Qt::NoFocus);
    overbutton->setFocusPolicy(Qt::NoFocus);
//禁止视窗获取焦点
    graphicsview->setFocusPolicy(Qt::NoFocus);
//禁止next视窗获取焦点
    nextgraphicsview->setFocusPolicy(Qt::NoFocus);
//禁止标签获取焦点
    scorelabel->setFocusPolicy(Qt::NoFocus);
//禁止微调框获取焦点
    gamedifficulty->setFocusPolicy(Qt::NoFocus);//还禁止了直接改变难度

}

MainWindow::~MainWindow() {}

//按钮槽函数
void MainWindow::ongamemodebuttonclicked(){
    if(!currentShape.isEmpty()) return;  //游玩时不能改变模式
    QString modetext=gamemodebutton->text();
    if(modetext=="低能模式"){
        gamemodebutton->setText("简单模式");
        SHAPES=SHAPES2;
    }
    else if(modetext=="简单模式"){
        gamemodebutton->setText("普通模式");
        SHAPES=SHAPES3;
    }
    else if(modetext=="普通模式"){
        gamemodebutton->setText("困难模式");
        SHAPES=SHAPES4;
    }
    else if(modetext=="困难模式"){
        gamemodebutton->setText("杂交模式");
        SHAPES=SHAPES5;
    }
    else {                                //杂交模式
        gamemodebutton->setText("低能模式");
        SHAPES=SHAPES1;
    }


}
void MainWindow::onstartbuttonclicked(){
    if(!gameTimer->isActive()&&(currentShape.isEmpty())){//防止多次点击和游戏暂停点击会重开
        score=0;//分数清零
        gameBoard.fill(QVector<QColor>(BOARD_WIDTH));//游戏界面全部填充为默认颜色：黑色
        gameTimer->start(UPDATE_INTERVAL);//定时开始
        newPiece();//生成新方块
    }
}
void MainWindow::onstopbuttonclicked(){
    if(!currentShape.isEmpty())//判断目前指针为不为空，防止游戏结束或未开始点击此按钮
     gameTimer->isActive() ? gameTimer->stop() : gameTimer->start();
}
void MainWindow::onoverbuttonclicked(){
    gameTimer->stop();//定时结束
    scene->clear();//清除游戏面板
    nextscene->clear();//清除下一个显示面板
    currentShape.clear();//清除此时的方块
    nextShape.clear();//清除下一个方块
    score=0;//分数清零
    update();//调用paintEvent函数重绘主界面
}

//更新分数显示槽函数
void MainWindow::updatescorelabel(){
    scorelabel->setText(QString("  Score ：%1").arg(score));
}

//更新游戏难度槽函数
void MainWindow::gamedifficultychanged(int value){
    if(value==6){UPDATE_INTERVAL=100;}
    else UPDATE_INTERVAL=1000/value;
    if(gameTimer->isActive()){
        gameTimer->start(UPDATE_INTERVAL);}//防止在计时暂停时触发计时

}


// 核心游戏逻辑


//生成新方块
void MainWindow::newPiece() {
    //当前方块
    if(currentShape.isEmpty()){  //判断是否要生成生成初始方块
    currentRotation = rand()%4;//随机旋转状态
    currentType = rand() % SHAPES.size();//随机方块类型
    currentColor = QColor::fromHsv(rand()%360, 255, 255);//随机方块颜色
    currentShape = SHAPES[currentType][currentRotation];//方块形状
    }
    else {
        currentShape=nextShape;
        currentRotation=nextRotation;
        currentType=nextType;
        currentColor=nextColor;
    }

    // 初始位置
    QPoint center(BOARD_WIDTH/2-1, 0);
    for(QPoint &p : currentShape) {//遍历生成方块四个点
        p += center;//对齐中心
    }

    QPoint nextcenter(2,1);
    if(!nextShape.isEmpty()){ //矫正对下一个方块对齐在显示窗口中心的操作
        for(QPoint &p : currentShape) {//遍历生成方块四个点
            p -= nextcenter;
        }
    }
    //下一个方块
    nextRotation = rand()%4;//随机旋转状态
    nextType = rand() % SHAPES.size();//随机方块类型
    nextColor = QColor::fromHsv(rand()%360, 255, 255);//随机方块颜色
    nextShape = SHAPES[nextType][nextRotation];//方块形状

    //优化下一个显示
    for(QPoint &p : nextShape){//遍历下一个方块四个点
        p +=nextcenter;//对齐中心
    }

    if(!tryMove(currentShape)) {//判断能否再合法移动了
        gameTimer->stop();
        currentShape.clear();//防止后续更新游戏场景依旧绘制当前方块
        nextShape.clear();//防止后续更新游戏场景依旧绘制下一个方块
        QMessageBox::information(nullptr,"提示","Game Over！");
    }
    updateScene();//实时更新游戏场景
    updatenextScene();//实时更新下一个方块显示
}

//确保方块正常移动
bool MainWindow::tryMove(const QVector<QPoint> &newShape) {
    for(const QPoint &p : newShape) {
        if(p.x() < 0 || p.x() >= BOARD_WIDTH ||
            p.y() >= BOARD_HEIGHT || p.y() <0 ||                        //出界
            (p.y() >=0 && gameBoard[p.y()][p.x()].isValid())) {//对应位置已有方块
            return false;//无法移动
        }
    }
    currentShape = newShape;//当前方块更新
    return true;//能移动
}

//消除满行
void MainWindow::clearLines() {
    for(int y = BOARD_HEIGHT-1; y >=0; --y) {  //遍历每行
        bool lineFull = true;
        for(int x=0; x<BOARD_WIDTH; ++x) {
            if(!gameBoard[y][x].isValid()) {
                lineFull = false;      //此行有空位
                break;
            }
        }
        if(lineFull) {     //满行
            gameBoard.remove(y);//清除此行
            gameBoard.prepend(QVector<QColor>(BOARD_WIDTH));//最上方插入一行空白行
            ++y; // 重新检查当前行（因为消除行上方的所有行下落一行）
            score+=10*gamedifficulty->value();//加分
            emit this->scorechanged();//发射信号改变分数显示
        }
    }
}

//固定方块
void MainWindow::fixToBoard() {
    for(const QPoint &p : currentShape) {  //遍历当前方块每个点
        if(p.y() >=0) gameBoard[p.y()][p.x()] = currentColor;//固定方块（即涂色）
    }
    clearLines();//固定完立即判断消除满行
}

//旋转方块
void MainWindow::rotateShape() {
    if (currentShape.isEmpty()) return; // 防止空指针

    // 计算下一个旋转状态
    int nextRotation = (currentRotation + 1) % 4;
    QVector<QPoint> newShape = SHAPES[currentType][nextRotation];

    // 计算中心点偏移
    QPoint offset = currentShape[0]-newShape[0];//计算中心点偏移量（由初始位置和左右与向下移动造成）
    //本质上是让第一个点重合，故看起来是以第一个点旋转的
    //减去newShape初始位置，再加上currentShape初始位置和位移（左右下位移）

    // 应用旋转并调整位置
    for (QPoint &p : newShape) {  //遍历旋转后方块的每个点
        p += offset;
    }

    if (tryMove(newShape)) {  //能合法旋转
        currentRotation = nextRotation;//为下一次旋转做准备
       // currentShape = newShape;  //（返回true时就已经更新）
    }
    updateScene();//更新游戏场景
}

//方块下落
void MainWindow::moveDown() {
    QVector<QPoint> newShape;
    for(const QPoint &p : currentShape) {//遍历
        newShape.append(p + QPoint(0,1));//尝试向下移一行
    }
    if(!tryMove(newShape)) {  //（返回true时已经更新currentshape）
        fixToBoard();//固定方块（到底）
        newPiece();//生成新方块
    }
    updateScene();//更新游戏场景
}

//左右移动方块
void MainWindow::tryMoveShape(int dx, int dy) {
    QVector<QPoint> newShape;
    for(const QPoint &p : currentShape) {
        newShape.append(p + QPoint(dx, dy));
    }
    tryMove(newShape);//能移动就更新
    updateScene();//更新游戏场景
}


// 键盘控制
void MainWindow::keyPressEvent(QKeyEvent *event) {
    switch(event->key()) {
    case Qt::Key_Left:
        tryMoveShape(-1, 0);//左移
        break;
    case Qt::Key_Right:
        tryMoveShape(1, 0);//右移
        break;
    case Qt::Key_Down:
        moveDown();//下落
        break;
    case Qt::Key_Up:
        rotateShape();//旋转
        break;
    case Qt::Key_Space:
        onstopbuttonclicked();//暂停
        break;
    }
}

//主窗口绘制
void MainWindow::paintEvent(QPaintEvent *event){
    QPainter scenepainter(this);//主窗口绘制背景色
    QRadialGradient skygradient(width()/2,height()/2,qMax(width(),height()));//径向渐变对象
    skygradient.setColorAt(0,QColor(120,120,120));
    skygradient.setColorAt(1,QColor(120,120,120));
    scenepainter.setBrush(skygradient);//设置画刷
    scenepainter.drawRect(rect());//rect获取主窗口矩形区域，画出矩形
}

//游戏界面绘制
void MainWindow::updateScene() {
    scene->clear();//清空游戏界面

    // 绘制游戏区域背景
    //scene->addRect(0, 0,BOARD_WIDTH*BLOCK_SIZE, BOARD_HEIGHT*BLOCK_SIZE,QPen(Qt::green), QBrush(Qt::black));
    QPixmap pixmap(":/scene-background/star.png");
    scene->setBackgroundBrush(pixmap);

    // 绘制已固定的方块
    for(int y = 0; y < BOARD_HEIGHT; ++y) {
        for(int x = 0; x < BOARD_WIDTH; ++x) {
            if(gameBoard[y][x].isValid()) {//指定位置颜色是否有效（默认颜色：黑色即为无效）
                scene->addRect(
                    x*BLOCK_SIZE, y*BLOCK_SIZE,//左上角坐标
                    BLOCK_SIZE-2, BLOCK_SIZE-2,//空出2在方块间留间隙
                    QPen(Qt::gray), QBrush(gameBoard[y][x]));
            }
        }
    }

    // 绘制当前方块
    if (currentShape.isEmpty()) return; // 防止空指针
    for(const QPoint &p : currentShape) {
        scene->addRect(
            p.x()*BLOCK_SIZE, p.y()*BLOCK_SIZE,
            BLOCK_SIZE-2, BLOCK_SIZE-2,
            QPen(Qt::white), QBrush(currentColor));
    }
}

//下一个方块绘制
void MainWindow::updatenextScene(){
    nextscene->clear();//清空显示

    //绘制显示背景
    //nextscene->addRect(0,0,BLOCK_SIZE*5,BLOCK_SIZE*5,QPen(Qt::green),QBrush(Qt::black));
    QPixmap nextpixmap(":/nextscene-background/outerspace.png");
    nextscene->setBackgroundBrush(nextpixmap);

    //绘制下一个方块
    if(nextShape.isEmpty()) return;//防止空指针
    for(const QPoint &p : nextShape) {
        nextscene->addRect(
            p.x()*BLOCK_SIZE, p.y()*BLOCK_SIZE,
            BLOCK_SIZE-2, BLOCK_SIZE-2,
            QPen(Qt::white), QBrush(nextColor));
    }
}
